package com.retail.bbq.cola.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retail.bbq.cola.constant.RedisConstant;
import com.retail.bbq.cola.entity.CustomerAgreementInfo;
import com.retail.bbq.cola.enums.DeleteStatusEnum;
import com.retail.bbq.cola.enums.TicketStatusEnum;
import com.retail.bbq.cola.service.CustomerAgreementInfoService;
import com.retail.bbq.cola.mapper.CustomerAgreementInfoMapper;
import com.retail.bbq.cola.service.WechatCustomerService;
import com.retail.bbq.cola.vo.ResultVO;
import com.retail.bbq.cola.vo.request.CustomerAgreementRequest;
import com.retail.bbq.cola.vo.request.SaveUserInfoRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author jarvis.zhang
* @description 针对表【customer_agreement_info(客户授权小程序协议签署表)】的数据库操作Service实现
* @createDate 2025-06-12 10:21:11
*/
@Service
@Slf4j
public class CustomerAgreementInfoServiceImpl extends ServiceImpl<CustomerAgreementInfoMapper, CustomerAgreementInfo>
    implements CustomerAgreementInfoService{

    @Resource
    private WechatCustomerService wechatCustomerService;

    @Override
    public ResultVO<?> agreement(CustomerAgreementRequest request) {
        agreement(request, String.format(RedisConstant.REDIS_KEY_USER_INFO_REPEAT + ":agreement", request.getOpenId(), request.getOneid()));
        return ResultVO.succ(Boolean.TRUE);
    }

    private void agreement(CustomerAgreementRequest request, String redisKey) {
        //授权时候需要再更新客户信息  要看流程
        //查询当前是否有客户信息，没有的话 保存客户信息
        wechatCustomerService.userInfoSaveOrUpdate(changeRequestToWechatCustomer(request), redisKey);
        CustomerAgreementInfo agreementInfo = new CustomerAgreementInfo();
        List<CustomerAgreementInfo> agreementInfos = baseMapper.queryAgreement(new CustomerAgreementRequest(
                request.getBuCode(),
                request.getSetupId(),
                request.getOpenId(),
                request.getOneid(),
                request.getLawsonId()
        ));

        if (CollectionUtil.isNotEmpty(agreementInfos)) {
            for (CustomerAgreementInfo info : agreementInfos) {
                BeanUtils.copyProperties(request, info);
                info.setUpdateTime(new Date());
            }
            this.updateBatchById(agreementInfos);
        } else {
            BeanUtils.copyProperties(request, agreementInfo);
            agreementInfo.setUpdateTime(new Date());
            this.save(agreementInfo);
        }
    }


    private SaveUserInfoRequest changeRequestToWechatCustomer(CustomerAgreementRequest request) {
        log.info("授权阶段用户信息,请求参数:{}", request);
        SaveUserInfoRequest saveUserInfoRequest = new SaveUserInfoRequest();
        saveUserInfoRequest.setOpenId(request.getOpenId());
        saveUserInfoRequest.setOneid(request.getOneid());
        saveUserInfoRequest.setEncryptMobile(request.getEncryptMobile());
        saveUserInfoRequest.setUnionId(request.getUnionId());
        saveUserInfoRequest.setNickName(request.getNickName());
        saveUserInfoRequest.setAvatarUrl(request.getAvatarUrl());
        saveUserInfoRequest.setStatus(request.getStatus());
        saveUserInfoRequest.setMobile(request.getMobile());
        saveUserInfoRequest.setCustomerId(request.getCustomerId());
        saveUserInfoRequest.setAgreementTime(request.getAgreementTime());
        saveUserInfoRequest.setAgreementInfo(request.getAgreementInfo());
        saveUserInfoRequest.setLawsonId(request.getLawsonId());
        return saveUserInfoRequest;
    }

    @Override
    public ResultVO<List<CustomerAgreementInfo>> queryAgreement(CustomerAgreementRequest request) {
        //查询当前是否有客户信息，没有的话 保存客户信息
        wechatCustomerService.userInfoSaveOrUpdate(
                changeRequestToWechatCustomer(request),
                String.format(RedisConstant.REDIS_KEY_USER_INFO_REPEAT + ":queryAgreement", request.getOpenId(), request.getOneid())
        );
        List<CustomerAgreementInfo> agreementInfos = baseMapper.queryAgreement(new CustomerAgreementRequest(
                request.getBuCode(),
                request.getSetupId(),
                request.getOpenId(),
                request.getOneid(),
                request.getLawsonId()
        ));
        if (CollectionUtils.isEmpty(agreementInfos)) {
            CustomerAgreementInfo agreementInfo = new CustomerAgreementInfo();
            BeanUtils.copyProperties(request, agreementInfo);
            agreementInfo.setAgreementStatus(0);
            agreementInfo.setTicketStatus(0);
            agreementInfo.setIsDel(DeleteStatusEnum.no_del.getCode());
            this.save(agreementInfo);
            return ResultVO.succ(Collections.singletonList(agreementInfo));
        }
        return ResultVO.succ(agreementInfos);
    }

    @Override
    public boolean updateTicket(CustomerAgreementRequest request, TicketStatusEnum ticketStatusEnum) {
        request.setTicketStatus(ticketStatusEnum.getCode());
        request.setAgreementStatus(null);
        agreement(request, String.format(RedisConstant.REDIS_KEY_USER_INFO_REPEAT + ":agreement", request.getOpenId(), request.getOneid()));
        return true;
    }
}




